﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NMLX_Data_Management
{
    public partial class AddProgram : Form
    {
        private int proId;
        private string proName;
        

        public AddProgram()
        {
            InitializeComponent();
            
        }

        public AddProgram(int proId, string proName)
        {
            InitializeComponent();
            this.proId = proId;
            this.proName = proName;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(openfolder));
            thread.ApartmentState = System.Threading.ApartmentState.STA;
            thread.Start();         
           
        }

        private void openfolder()
        {
            this.textBox2.Enabled = true;
            if (string.IsNullOrEmpty( textBox1.Text))
            {
                return;
            }
            using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
            {
                folderBrowserDialog.Description = "请选择文件路径";
                folderBrowserDialog.SelectedPath = AppDomain.CurrentDomain.BaseDirectory + "NMLX_Projects\\" + comboBox1.Text;
                try
                {
                    if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                    {
                        //获取到上传路径
                        string path = folderBrowserDialog.SelectedPath;
                        textBox2.Text = folderBrowserDialog.SelectedPath + "\\" + textBox1.Text;


                    }
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void AddProgram_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“nMLX_Data_MDataSet.Project”中。您可以根据需要移动或删除它。
            //this.projectTableAdapter.Fill(this.nMLX_Data_MDataSet.Project);
            CheckForIllegalCrossThreadCalls = false;
            //this.textBox1.Focus();
            this.textBox1.TabIndex = 0;
            try
            {
                IList<Project> infoList = new List<Project>();
                Project project = new Project() { proId = this.proId.ToString(), proName = this.proName };
                infoList.Add(project);
                this.comboBox1.DataSource = infoList;
                this.comboBox1.DisplayMember = "proName";
                this.comboBox1.ValueMember = "proId";
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                return;
            }
            try
            {
                var proid = this.comboBox1.SelectedValue;
                var proname = this.comboBox1.Text;
                var program = this.textBox1.Text;
                var path = this.textBox2.Text;
                int index = path.IndexOf("NMLX_Projects");
                path = path.Substring(index);
                //var p = AppDomain.CurrentDomain.BaseDirectory;
                //path = p + path;
                //DirectoryInfo theFolder = new DirectoryInfo(path);
                Directory.CreateDirectory(path);
                string sql = string.Format("insert into projectdetail" +
                    " (ProjectID,ProjectName,ProgramName,path) values ('{0}','{1}','{2}','{3}')" +
                    "",proid,proname, program, path);
                if (NMLX.DBUtility.DbHelperOleDb.ExecuteSql(sql) > 0)
                {
                    MessageBox.Show("添加成功","提示");
                }
                else
                {
                    MessageBox.Show("失败");
                }
                this.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
          
           
        }

        private void AddProgram_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.textBox2.Enabled = true;
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                return;
            }
            string path = AppDomain.CurrentDomain.BaseDirectory + "NMLX_Projects\\" + comboBox1.Text;
            textBox2.Text = AppDomain.CurrentDomain.BaseDirectory + "NMLX_Projects\\" + comboBox1.Text + "\\" + textBox1.Text;
        }
    }
}
